import strfile
import os
import pdb

def LZdecompr(buff,outlen):
    uncompr=[]
    iSrc=0
    while iSrc<len(buff):
        code=ord(buff[iSrc])
        iSrc+=1
        for i in range(8):
            if (code>>i) & 1:
                uncompr.append(buff[iSrc])
                iSrc+=1
                if iSrc>=len(buff) or len(uncompr)>=outlen: break
            else:
                count=(ord(buff[iSrc]) & 0xF)+2
                pos=(ord(buff[iSrc])>>4) + (ord(buff[iSrc+1])<<4)
                iSrc+=2
                for j in range(count):
                    uncompr.append(uncompr[-pos])
                if iSrc>=len(buff) or len(uncompr)>=outlen: break
    return ''.join(uncompr)

decode_table=[
    0x8B, 0xE5, 0x5D, 0xC3, 0xA1, 0xE0, 0x30, 0x44, 0x00, 0x85, 0xC0, 0x74, 0x09, 0x5F, 0x5E, 0x33, 0xC0, 0x5B, 0x8B, 0xE5, 0x5D, 0xC3, 0x8B, 0x45, 0x0C, 0x85, 0xC0, 0x75, 0x14, 0x8B, 0x55, 0xEC,
    0x83, 0xC2, 0x20, 0x52, 0x6A, 0x00, 0xE8, 0xF5, 0x28, 0x01, 0x00, 0x83, 0xC4, 0x08, 0x89, 0x45, 0x0C, 0x8B, 0x45, 0xE4, 0x6A, 0x00, 0x6A, 0x00, 0x50, 0x53, 0xFF, 0x15, 0x34, 0xB1, 0x43, 0x00,
    0x8B, 0x45, 0x10, 0x85, 0xC0, 0x74, 0x05, 0x8B, 0x4D, 0xEC, 0x89, 0x08, 0x8A, 0x45, 0xF0, 0x84, 0xC0, 0x75, 0x78, 0xA1, 0xE0, 0x30, 0x44, 0x00, 0x8B, 0x7D, 0xE8, 0x8B, 0x75, 0x0C, 0x85, 0xC0,
    0x75, 0x44, 0x8B, 0x1D, 0xD0, 0xB0, 0x43, 0x00, 0x85, 0xFF, 0x76, 0x37, 0x81, 0xFF, 0x00, 0x00, 0x04, 0x00, 0x6A, 0x00, 0x76, 0x43, 0x8B, 0x45, 0xF8, 0x8D, 0x55, 0xFC, 0x52, 0x68, 0x00, 0x00,
    0x04, 0x00, 0x56, 0x50, 0xFF, 0x15, 0x2C, 0xB1, 0x43, 0x00, 0x6A, 0x05, 0xFF, 0xD3, 0xA1, 0xE0, 0x30, 0x44, 0x00, 0x81, 0xEF, 0x00, 0x00, 0x04, 0x00, 0x81, 0xC6, 0x00, 0x00, 0x04, 0x00, 0x85,
    0xC0, 0x74, 0xC5, 0x8B, 0x5D, 0xF8, 0x53, 0xE8, 0xF4, 0xFB, 0xFF, 0xFF, 0x8B, 0x45, 0x0C, 0x83, 0xC4, 0x04, 0x5F, 0x5E, 0x5B, 0x8B, 0xE5, 0x5D, 0xC3, 0x8B, 0x55, 0xF8, 0x8D, 0x4D, 0xFC, 0x51,
    0x57, 0x56, 0x52, 0xFF, 0x15, 0x2C, 0xB1, 0x43, 0x00, 0xEB, 0xD8, 0x8B, 0x45, 0xE8, 0x83, 0xC0, 0x20, 0x50, 0x6A, 0x00, 0xE8, 0x47, 0x28, 0x01, 0x00, 0x8B, 0x7D, 0xE8, 0x89, 0x45, 0xF4, 0x8B,
    0xF0, 0xA1, 0xE0, 0x30, 0x44, 0x00, 0x83, 0xC4, 0x08, 0x85, 0xC0, 0x75, 0x56, 0x8B, 0x1D, 0xD0, 0xB0, 0x43, 0x00, 0x85, 0xFF, 0x76, 0x49, 0x81, 0xFF, 0x00, 0x00, 0x04, 0x00, 0x6A, 0x00, 0x76,
]

def decode(buff):
    buff.seek(0x20)
    deoff=buff.readu32()
    buff.seek(0x28)
    size=buff.readu32()
    newb=[buff[0:deoff]]
    for i in range(size):
        newb.append(chr(ord(buff[i+deoff])^decode_table[i&0xff]))
    compr=strfile.MyStr(''.join(newb[1:]))
    compr.seek(4)
    uncompr=LZdecompr(compr[8:],compr.readu32())
    return ''.join([newb[0],compr[0:8],uncompr])

fs=file('seen.txt','rb')
idxes=strfile.MyStr(fs.read(10000*8))
if 'seen' not in os.listdir('.'):
    os.mkdir('seen')
for i in range(10000):
    off=idxes.readu32()
    size=idxes.readu32()
    if off!=0:
        newfile=file('seen\\seen%04d.txt'%i,'wb')
        fs.seek(off)
        newfile.write(decode(strfile.MyStr(fs.read(size))))
        newfile.close()
        print '%04d Success'%i
print 'Success'
